import numpy as np
from datetime import datetime, timedelta

def generate_temperature_data():
    # 生成一天内的温度数据，每小时一个数据点
    np.random.seed(42)  # 设置随机种子确保可重复性
    
    # 创建24小时的时间点
    start_time = datetime.now().replace(hour=0, minute=0, second=0)
    times = [(start_time + timedelta(hours=i)).strftime('%H:%M:%S') for i in range(24)]
    
    # 生成温度数据 (基准温度20度，添加随机波动)
    base_temp = 20
    temperatures = base_temp + 5 * np.sin(np.linspace(0, 2*np.pi, 24)) + np.random.normal(0, 1, 24)
    
    # 生成湿度数据
    humidity = 50 + 10 * np.sin(np.linspace(0, 2*np.pi, 24)) + np.random.normal(0, 2, 24)
    
    # 保存数据到文件
    with open('sensor_data.txt', 'w') as f:
        f.write('Time Temperature Humidity\n')  # 添加表头
        for t, temp, hum in zip(times, temperatures, humidity):
            f.write(f'{t} {temp:.2f} {hum:.2f}\n')

if __name__ == '__main__':
    generate_temperature_data()
    print("数据文件已生成：sensor_data.txt")